// -----------------------------------------------------------------------
//  <copyright file="CoreModule.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2022 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2022-11-20 16:49</last-date>
// -----------------------------------------------------------------------


using DaprPlus.Dependency;
using DaprPlus.Filters;
using DaprPlus.Properties;
using DaprPlus.Settings.Domain.Services;

using OSharp.Caching;
using OSharp.Data.Snows;
using OSharp.Localized;


namespace DaprPlus;

/// <summary>
/// DaprPlus核心模块，0号模块
/// </summary>
[LocalizedDescription("String_Module_Core", typeof(ResDaprPlus))]
public class CoreModule : ModuleBase
{
    /// <summary>
    /// 获取 模块级别，级别越小越先启动
    /// </summary>
    public override ModuleLevel Level => ModuleLevel.Core;

    /// <summary>
    /// 将模块服务添加到依赖注入服务容器中
    /// </summary>
    /// <param name="services">依赖注入服务容器</param>
    /// <returns></returns>
    public override IServiceCollection AddServices(IServiceCollection services)
    {
        DistributedCacheExtensions.ToJsonFunc = obj => obj.ToTextJson();
        DistributedCacheExtensions.FromJsonFunc = (val, type) => val.FromTextJson(type);

        services.GetOrAddSingletonInstance<IDaprPlusBuilder>(() => new DaprPlusBuilder(services));

        //Dependency
        services.AddTransient(typeof(Lazy<>), typeof(Lazier<>));
        services.AddScoped<ScopedDictionary>();
        services.AddScoped<ISettingStore, SettingStore>();

        services.AddSingleton<IFilterService, FilterService>();
        services.AddSingleton<IFilterValueProvider, UserFilterValueProvider>();
        services.TryAddSingleton<ICancellationTokenProvider, NoneCancellationTokenProvider>();

        IdHelper.SetIdGenerator(new IdGeneratorOptions(1));

        return base.AddServices(services);
    }
}
